package com.free.files.fdfs;

import com.free.exception.SystemException;
import org.apache.commons.lang3.StringUtils;
import org.csource.fastdfs.*;

/**
 * <p>
 * 功能概述:
 * </p>
 * <p>
 * 功能详述:
 * </p>
 */
public class FastdfsConfiguer {
    /*========================================================================*
     *                         Public Fields (公共属性)
     *========================================================================*/

    /*========================================================================*
     *                         Private Fields (私有属性)
     *========================================================================*/

    private static final String CONFIG_FILE = "fastdfs_client.conf";

    private final static Object lock = new Object();

    /**
     * 跟踪器(通过跟踪器获得存储节点)
     */
    private TrackerClient tracker;


    private static FastdfsConfiguer _instance;

    /*========================================================================*
     *                         Construct Methods (构造方法)
     *========================================================================*/
    public FastdfsConfiguer(TrackerClient tracker){
        this.tracker = tracker;
    }

    /*========================================================================*
     *                         Public Methods (公有方法)
     *========================================================================*/

    public static FastdfsConfiguer instance() throws SystemException
    {
        _instance = instance(CONFIG_FILE);
        return _instance;
    }

    public static FastdfsConfiguer instance(String conf) throws SystemException{
        if(StringUtils.isEmpty(conf))
        {
            return null;
        }
        synchronized(lock)
        {
            try
            {
                ClientGlobal.init(conf);
                FastdfsConfiguer helper = new FastdfsConfiguer(new TrackerClient());
                return helper;
            }
            catch (Throwable e)
            {
                throw new SystemException("获得Fastdfs配置信息失败", e);
            }
        }
    }

    /**
     * 获得栈对象
     * @return
     */
    public StorageClient1 getStorageClient1() throws SystemException
    {
        try
        {
            TrackerServer trackerServer =  this.tracker.getConnection();
            StorageClient1 client = new StorageClient1(trackerServer, null);
            return client;
        }
        catch(Exception e)
        {
            throw new SystemException("获得Fastdfs存放目录失败", e);
        }
    }

    public StorageClient getStorageClient() throws SystemException
    {
        try
        {
            TrackerServer trackerServer =  this.tracker.getConnection();
            StorageClient client = new StorageClient(trackerServer, null);
            return client;
        }
        catch(Exception e)
        {
            throw new SystemException("获得Fastdfs存放目录失败", e);
        }
    }


    /*========================================================================*
     *                         Private Methods (私有方法)
     *========================================================================*/
}
